<?php

/**
 * @Author: king
 * @Date:   2019-05-04 19:12:28
 * @Last Modified by:   king
 * @Last Modified time: 2019-05-12 12:16:51
 */
namespace App\Models\Api;
use App\Exceptions\ApiException;
use App\Models\Api\Base;
use Cache;
/**
 * 用户习题进度模型
 * Class LanmeiSectionExerciseProgress
 * @package app\common\model
 */
class LanmeiSectionExerciseProgress extends Base
{

    const TAG = 'exercise';
    const USER_EXERCISE_PROGRESS = ':user:exercise:progress:';
    const CACHE_TIME = 86400 * 7;//用户章节习题进度缓存
    protected $table = 'exercise_progress';

    /**
     * 获取用户习题完成状态
     * @param array $map
     * @return array|bool|mixed
     */
    public function getUserExerciseProgress($map = [])
    {
        $csid = $map['csid'];
        $uid = $map['uid'];

        $data = false;

        if ($this->cache_on >= 1 && Cache::has($uid . self::USER_EXERCISE_PROGRESS . $csid)) {
            $data = Cache::get($uid . self::USER_EXERCISE_PROGRESS . $csid);
        }

        if ($data === false) {
            try {

                $where = ['csid' => $csid, 'uid' => $uid];

                $data = $this->where($where)->get(['exercise_status']);

                if ($this->cache_on >= 1) {
                    Cache::tags(self::TAG)->set($uid . self::USER_EXERCISE_PROGRESS . $csid, $data, self::CACHE_TIME);
                }

            } catch (\Exception $e) {
                throw new ApiException('获取习题进度失败', config('code.error'));
            }
        }
        return $data;
    }

    /**
     * 新增用户习题完成状态
     * @param array $map
     * @return bool
     */
    public function addExerciseProgress($map = [])
    {
        $uid = $map['uid'];
        $csid = $map['csid'];
        $exercise_status = $map['status'];

        $arr = [
            'uid' => $uid,
            'csid' => $csid,
            'exercise_status' => $exercise_status
        ];

        try {
            $this->insert($arr);

            if ($this->cache_on >= 1) {
                $status = [
                    '0' => $exercise_status
                ];

                Cache::tags(self::TAG)->set($uid . self::USER_EXERCISE_PROGRESS . $csid, $status, self::CACHE_TIME);
            }
        } catch (\Exception $e) {
            throw new ApiException('保存习题进度失败', config('code.error'));
        }

        return true;
    }


    /**
     * 修改用户习题完成状态
     * @param array $map
     * @return bool
     */
    public function editExerciseProgress($map = [])
    {
        $uid = $map['uid'];
        $csid = $map['csid'];
        $exercise_status = $map['status'];

        try {
            $this->insert(['exercise_status' => $exercise_status], ['uid' => $uid, 'csid' => $csid]);

            if ($this->cache_on >= 1) {
                $status = [
                    '0' => $exercise_status
                ];

                Cache::tags(self::TAG)->set($uid . self::USER_EXERCISE_PROGRESS . $csid, $status, self::CACHE_TIME);
            }

        } catch (\Exception $e) {
            throw new ApiException('更新习题进度失败', config('code.error'));
        }

        return true;
    }

}
